<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="年月" prop="ym">
        <el-date-picker
          v-model="queryParams.ym"
          value-format="yyyy-MM"
          type="month"
          @change="change_ym"
          placeholder="請選擇年月"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="跳過的日期" prop="skipHolidays">
        <el-date-picker
          type="dates"
          v-model="queryParams.skipHolidays"
          placeholder="請選擇跳過的日期"
          :picker-options="pickerOptions"
          :default-value="default_value"
          value-format="yyyy-MM-dd"
          :disabled="disabled_skipHolidays"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="時間段" prop="timePeriod">
        <el-time-picker
          is-range
          value-format="HH:mm"
          v-model="queryParams.timePeriod"
          range-separator="至"
          start-placeholder="開始時間"
          end-placeholder="結束時間"
          placeholder="選擇時間范围"
        >
        </el-time-picker>
      </el-form-item> -->
      <el-form-item label="是否生效" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="請選擇是否生效" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <!-- sys_yes_no -->
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:rule:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:rule:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:rule:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:rule:export']"
          >導出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ruleList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="年月" align="center" prop="ym" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.ym }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="200" label="跳過的日期" align="center">
        <template slot-scope="scope">
          <span v-for="(item,index) in JSON.parse(scope.row.skipHolidays)"> 
            {{item}}
            <span v-if="index!=JSON.parse(scope.row.skipHolidays).length-1">,</span>
          </span>
        
        </template>
      </el-table-column>
      <el-table-column
        label="時間段"
        align="center"
        prop="timePeriod"
        width="180"
      >
        <template slot-scope="scope">
          <p v-for="item in JSON.parse(scope.row.timePeriod)">
            <span>{{ item.start }}</span>
            <span>-</span>
            <span>{{ item.end }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="是否生效" align="center" prop="enable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enable" />
        </template>
      </el-table-column>
      <el-table-column label="備註" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:rule:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:rule:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约规则
按月度设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年月" prop="ym">
          <el-date-picker
            clearable
            v-model="form.ym"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇年月"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跳過的日期" prop="skipHolidays">
          <el-date-picker
            clearable
            v-model="form.skipHolidays"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇跳過的日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="時間段JSON數組" prop="timePeriod">
          <el-date-picker
            clearable
            v-model="form.timePeriod"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇時間段JSON數組"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否生效" prop="enable">
          <el-input v-model="form.enable" placeholder="請輸入是否生效" />
        </el-form-item>
        <el-form-item label="删除狀態" prop="delFag">
          <el-input v-model="form.delFag" placeholder="請輸入删除狀態" />
        </el-form-item>
        <el-form-item label="備註" prop="remark">
          <el-input v-model="form.remark" placeholder="請輸入備註" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRule,
  getRule,
  delRule,
  addRule,
  updateRule,
} from "@/api/biz/rule";

export default {
  name: "Rule",
  dicts: ["sys_yes_no"],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 预约规则
      // 按月度设置表格數据
      ruleList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ym: null,
        skipHolidays: null,
        timePeriod: null,
        enable: null,
        delFag: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      // 表单校验
      rules: {},
      disabled_skipHolidays: true,
    };
  },
  computed: {
    pickerOptions() {
      let that = this;
      return {
        disabledDate(time) {
          if (!that.queryParams.ym) {
            return false;
          }
          let days = that
            .$moment([
              that.queryParams.ym.split("-")[0],
              that.queryParams.ym.split("-")[1] - 1,
            ])
            .daysInMonth();
          let dateStart =
            new Date(that.queryParams.ym + "-01").getTime() -
            24 * 60 * 60 * 1000;
          // 結束日期
          let dateEnd = new Date(that.queryParams.ym + "-" + days).getTime();
          // 在開始日期和結束日期内可以選擇，其他不可選擇
          return time.getTime() < dateStart || time.getTime() > dateEnd;
          // 開始日期
        },
      };
    },
    default_value() {
      return this.queryParams.ym;
    },
  },
  created() {
    this.getList();
  },
  methods: {
    change_ym() {
      console.log(22222);
      this.queryParams.skipHolidays = null;
      if (this.queryParams.ym) {
        this.disabled_skipHolidays = false;
      } else {
        this.disabled_skipHolidays = true;
      }
    },
    //查询预约规则
    //按月度设置列表
    getList() {
      this.loading = true;
      listRule(this.queryParams).then((response) => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ym: null,
        skipHolidays: null,
        timePeriod: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        enable: null,
        delFag: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams);
      console.log(this.form);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({ path: "/reservation/rule/add" });
      // this.open = true;
      // this.title = "添加预约规则按月度设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.$router.push({ path: "/reservation/rule/edit", query:{id:id} });
      // getRule(id).then((response) => {
      //   this.form = response.data;
      //   this.$router.push({ path: "/reservation/index/edit", query: id });
      //   // this.open = true;
      //   // this.title = "修改预约规则按月度设置";
      // });
    },
    handleDetail(row){
      const id = row.id || this.ids;
      this.$router.push({ path: "/reservation/rule/detail", query:{id:id} });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除预约规则按月度设置編號為"' + ids + '"的數据项？')
        .then(function () {
          return delRule(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 導出按钮操作 */
    handleExport() {
      this.download(
        "biz/rule/export",
        {
          ...this.queryParams,
        },
        `rule_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
